package dataPersistence;

import hitExceptions.InvalidOperationException;

import java.io.*;

import coreModel.HIT;
import coreModel.SerializableImplementer;



/**
 * Serializer class handles saving all the objects to file and loading the objects from file.
 * @author The Fantastic 4
 *
 */
@SuppressWarnings("serial")
public class Serializer extends SerializableImplementer{

	String defaultFile;

	/**
	 * Constructor 
	 * @param defaultFile default file is saving or loading and a file is not specified
	 */
	public Serializer(String defaultFile)
	{
		this.defaultFile=defaultFile;
	}

	/**
	 * serializes the hit object with the default file location which is given at object
	 *  construction
	 * @param hit HIT
	 * @param filename String
	 * @throws InvalidOperationException 
	 * @throws FileNotFoundException 
	 */
	public void saveObjects(HIT hit) throws InvalidOperationException 
	{
		saveObjects(hit,defaultFile);
	}

	/**
	 * serializes the hit object passed in to an external file
	 * @param hit HIT
	 * @param filename String
	 * @throws InvalidOperationException 
	 */
	public void saveObjects(HIT hit, String fileName) throws InvalidOperationException 
	{
		File file= new File(fileName);
		try
		{
			file.createNewFile();
			FileOutputStream fStream= new FileOutputStream(file);
			ObjectOutputStream oStream = new ObjectOutputStream(fStream);
			oStream.writeObject(hit);
			oStream.flush();
			oStream.close();
		}
		catch(IOException io)
		{
			System.err.println("IO error Serializing Home Inventory Tracker object");
			io.printStackTrace();
			throw new InvalidOperationException("Could not serialize object");
		}
		catch(Exception e)
		{
			System.err.println("Error Serializing Home Inventory Tracker object");
			e.printStackTrace();
			throw new InvalidOperationException("Could not serialize object");
		}

	}

	/**
	 * deserializes the hit object from the default file and sets the hit Instance in the HIT
	 * @throws InvalidOperationException 
	 */
	public void loadObjects() throws InvalidOperationException
	{
		loadObjects(defaultFile);
	}

	/**
	 * deserializes the hit object from the given file and sets the hit instance
	 * @param filename file to load
	 * @throws InvalidOperationException 
	 * 
	 */
	public void loadObjects(String fileName) throws InvalidOperationException 
	{
		File file = new File(fileName);
		HIT hit=null;
		if(!file.exists())
		{

			return;
		}
		try
		{
			if(!file.exists())
			{
				throw new FileNotFoundException();
			}
			FileInputStream fStream= new FileInputStream(file);
			ObjectInputStream objStream;
			objStream = new ObjectInputStream(fStream);
			hit=(HIT)objStream.readObject();
		} 
		//check for load file not being found
		catch(FileNotFoundException e)
		{
			System.err.println("Error: file not found");
			
		}
		//checking for IO error
		catch (IOException e)
		{
			System.err.println("IO error loading serialized file");
		
		}
		//checking for serializer error
		catch(ClassNotFoundException e)
		{
			System.err.println("Class Not Found error loading serialized file");
		}
		//catch all
		catch(Exception e)
		{
			System.err.println("Error: Unknown error");
			
		}


		if(hit!=null)
			HIT.setInstance(hit);//set the hit instance


	}






}

